package com.example.studyllm.ollama;

import cn.hutool.json.JSONUtil;
import com.example.studyllm.utils.HttpClientUtils;
import com.example.studyllm.utils.HttpQueryParams;
import com.example.studyllm.vo.base.Result;
import dev.langchain4j.community.model.dashscope.QwenChatModel;
import dev.langchain4j.community.model.dashscope.QwenStreamingChatModel;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import dev.langchain4j.model.image.ImageModel;
import dev.langchain4j.model.ollama.OllamaChatModel;
import dev.langchain4j.model.ollama.OllamaStreamingChatModel;
import dev.langchain4j.service.AiServices;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @author wk
 */
@RestController
@RequestMapping("/ollama")
public class ChatController {

    @GetMapping(value="/chatStream", produces = "text/event-stream")
    public Flux<String> chatStream(){
        String message = "要使用io.github.czelabueno.jai.workflow.node.Node应该在pom中引入哪个依赖";
        StreamingChatLanguageModel model = buildStreamModel();
        Assistant assistant = AiServices.builder(Assistant.class)
                .streamingChatLanguageModel(model)
                .chatMemory(MessageWindowChatMemory.withMaxMessages(10))
              //  .tools(new WetherTool())
                .build();
        return assistant.chatStream(message);
    }

    @GetMapping("/testHrttp")
    public String testHrttp(){
        String url = "http://localhost:8888/ollama/hello";
        return JSONUtil.toJsonStr(HttpClientUtils.sendGet(HttpQueryParams.builder().url(url).build()));
    }
    @GetMapping("/hello")
    public Result sayhello(){
        return Result.success("hello");
    }

    private ChatLanguageModel buildTongyiModel(){
        return QwenChatModel.builder()
                .modelName("qwq-32b-preview")
                .apiKey("sk-0c9ec11299e04a8c95434b5d083a7d54")
                .build();
    }

    private StreamingChatLanguageModel buildTongyiStreamModel(){
        return QwenStreamingChatModel.builder()
                .modelName("qwq-32b-preview")
                .apiKey("sk-0c9ec11299e04a8c95434b5d083a7d54")
                .build();
    }

    private StreamingChatLanguageModel buildStreamModel(){
        return OllamaStreamingChatModel.builder()
                .baseUrl("http://60.190.170.54:11434")
                .modelName("deepseek-r1:32b")
                .temperature(0.1)
                .logRequests(true)
                .logResponses(true)
                .build();
    }



    private ChatLanguageModel buildModel(){
        return OllamaChatModel.builder()
                .baseUrl("http://47.109.192.172:11434")
                .modelName("llama3.2")
                .temperature(0.1)
                .build();
    }
}
